home *** CD-ROM | disk | FTP | other *** search
/ Mastering Web Site Development / Microsoft Mastering Web Site Development (Microsoft) (1997).iso / Labs / StateUFinal / controls / mascot.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-24  |  3.9 KB  |  133 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.MediaTracker;
  6. import java.awt.Rectangle;
  7. import java.awt.image.ImageObserver;
  8.  
  9. public class mascot extends Applet implements Runnable {
  10.    private Thread m_mascot;
  11.    private Graphics m_Graphics;
  12.    private Image[] m_Images;
  13.    private int m_nCurrImage;
  14.    private int m_nImgWidth;
  15.    private int m_nImgHeight;
  16.    private boolean m_fAllLoaded;
  17.    private final int NUM_IMAGES = 18;
  18.    public int speed;
  19.    public int direction;
  20.  
  21.    public void stop() {
  22.       if (this.m_mascot != null) {
  23.          this.m_mascot.stop();
  24.          this.m_mascot = null;
  25.       }
  26.  
  27.    }
  28.  
  29.    public void paint(Graphics g) {
  30.       if (this.m_fAllLoaded) {
  31.          Rectangle r = g.getClipRect();
  32.          g.clearRect(r.x, r.y, r.width, r.height);
  33.          this.displayImage(g);
  34.       } else {
  35.          g.drawString("Loading images...", 10, 20);
  36.       }
  37.  
  38.    }
  39.  
  40.    public void destroy() {
  41.    }
  42.  
  43.    private void displayImage(Graphics g) {
  44.       if (this.m_fAllLoaded) {
  45.          g.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_nImgWidth) / 2, (((Component)this).size().height - this.m_nImgHeight) / 2, (ImageObserver)null);
  46.       }
  47.    }
  48.  
  49.    public void stopMeUp() {
  50.       this.m_mascot.suspend();
  51.    }
  52.  
  53.    public void startMeUp() {
  54.       this.m_mascot.resume();
  55.    }
  56.  
  57.    public void start() {
  58.       if (this.m_mascot == null) {
  59.          this.m_mascot = new Thread(this);
  60.          this.m_mascot.start();
  61.       }
  62.  
  63.    }
  64.  
  65.    public String getAppletInfo() {
  66.       return "Name: mascot\r\n" + "Author: MS\r\n" + "Created with Microsoft Visual J++ Version 1.1";
  67.    }
  68.  
  69.    public void run() {
  70.       this.m_nCurrImage = 0;
  71.       if (!this.m_fAllLoaded) {
  72.          ((Component)this).repaint();
  73.          this.m_Graphics = ((Component)this).getGraphics();
  74.          this.m_Images = new Image[18];
  75.          MediaTracker tracker = new MediaTracker(this);
  76.          int i = 1;
  77.  
  78.          do {
  79.             String strImage = "images/img00" + (i < 10 ? "0" : "") + i + ".gif";
  80.             this.m_Images[i - 1] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), strImage);
  81.             tracker.addImage(this.m_Images[i - 1], 0);
  82.             ++i;
  83.          } while(i <= 18);
  84.  
  85.          try {
  86.             tracker.waitForAll();
  87.             this.m_fAllLoaded = !tracker.isErrorAny();
  88.          } catch (InterruptedException var5) {
  89.          }
  90.  
  91.          if (!this.m_fAllLoaded) {
  92.             this.stop();
  93.             this.m_Graphics.drawString("Error loading images!", 10, 40);
  94.             return;
  95.          }
  96.  
  97.          this.m_nImgWidth = this.m_Images[0].getWidth(this);
  98.          this.m_nImgHeight = this.m_Images[0].getHeight(this);
  99.       }
  100.  
  101.       ((Component)this).repaint();
  102.  
  103.       while(true) {
  104.          try {
  105.             this.displayImage(this.m_Graphics);
  106.             if (this.direction == 1) {
  107.                ++this.m_nCurrImage;
  108.                if (this.m_nCurrImage == 18) {
  109.                   this.m_nCurrImage = 0;
  110.                }
  111.             } else {
  112.                this.m_nCurrImage += -1;
  113.                if (this.m_nCurrImage == -1) {
  114.                   this.m_nCurrImage = 17;
  115.                }
  116.             }
  117.  
  118.             Thread.sleep((long)this.speed);
  119.          } catch (InterruptedException var6) {
  120.             this.stop();
  121.          }
  122.       }
  123.    }
  124.  
  125.    public void init() {
  126.       ((Applet)this).resize(320, 240);
  127.       String at = ((Applet)this).getParameter("speed");
  128.       this.speed = at != null ? Integer.valueOf(at) : 50;
  129.       at = ((Applet)this).getParameter("direction");
  130.       this.direction = at != null ? Integer.valueOf(at) : 1;
  131.    }
  132. }
  133.